package gdut.imis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gdut.imis.Utils.Result;
import gdut.imis.domain.ClassStudentRel;
import gdut.imis.domain.Course;
import gdut.imis.domain.CourseStudentRel;
import gdut.imis.enums.Status;
import gdut.imis.mapper.CourseMapper;
import gdut.imis.service.ClassStudentRelService;
import gdut.imis.service.CourseService;
import gdut.imis.service.CourseStudentRelService;
import gdut.imis.vo.CourseVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/7 18:12
 */
@Service
@RequiredArgsConstructor
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    private final CourseMapper courseMapper;

    private final CourseStudentRelService courseStudentRelService;

    private final ClassStudentRelService classStudentRelService;

    /***
     * 新增课程档案(拥有course权限)
     * @param: course
     * @return Result
     */
    @Override
    @Transactional
    public Result addCourse(CourseVo courseVo) {
        if (Objects.isNull(courseVo)) {
            return Result.errorWithArgs(Status.REQUEST_PARAMS_NOT_VALID_ERROR, "course");
        }
        //判断同一个班级是否已经有该门课程了，不允许同一个班级有名字相同的课程
        List<Course> courseList = courseMapper.ifHasSameClassCourse(courseVo.getClassId(), courseVo.getCourseName());
        if (!CollectionUtils.isEmpty(courseList)) {
            return Result.errorWithArgs(Status.FAILED, "存在同一个班级且同名的课程了");
        }
        Course course = new Course();
        BeanUtils.copyProperties(courseVo, course);
        boolean flag = this.save(course);
        if (flag) {
            //查询出该课程所在班级所有学生id,并构建学生与课程绑定对象列表存储到数据库course_student_rel
            LambdaQueryWrapper<ClassStudentRel> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ClassStudentRel::getClassId, courseVo.getClassId());
            List<ClassStudentRel> classStudentRelList = classStudentRelService.list(queryWrapper);
            //创建学生与课程绑定对象列表
            List<CourseStudentRel> courseStudentRelList = classStudentRelList.stream().map(classStudentRel -> {
                CourseStudentRel courseStudentRel = new CourseStudentRel();
                courseStudentRel.setCourseId(course.getId());
                courseStudentRel.setStudentId(classStudentRel.getStudentId());
                return courseStudentRel;
            }).collect(Collectors.toList());
            boolean flag2 = courseStudentRelService.saveBatch(courseStudentRelList);
            if (flag2) {
                return Result.success();
            }
            return Result.errorWithArgs(Status.FAILED, "新增课程档案失败，数据库插入异常");
        }
        throw new RuntimeException("新增课程档案失败,数据库插入异常");
    }


    /***
     * 根据学生id查询学生上的第一节课的信息
     * @param: studentId
     * @return Result
     */
    @Override
    public Result getStudentFirstCourse(Long studentId) {
        CourseVo courseVo = courseMapper.getStudentFirstCourse(studentId);
        if (Objects.isNull(courseVo)) {
            return Result.errorWithArgs(Status.FAILED,"查询失败");
        }
        return Result.success(courseVo);
    }

    /***
     * 根据学生id分页查询学生拥有的课程
     * @param: studentId
     * @param: pageNo
     * @param: pageSize
     * @return Result
     */
    @Override
    public Result getPageCoursesByStuId(Long studentId, Integer pageNo, Integer pageSize) {
        Page<CourseVo> page = new Page<>();
        page.setCurrent(pageNo);
        page.setSize(pageSize);

        Page<CourseVo> resPage = courseMapper.getPageCoursesByStuId(studentId,page);
        if (resPage.getTotal() > 0){
            return Result.success(resPage);
        }
        return Result.errorWithArgs(Status.FAILED,"没有查询到任何课程信息");
    }
}
